<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Shop_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url','form','string','file')); 
	}
		
		
	public function tambah_video($data)
	{			
		$this->db->insert('video', $data);
	}
	
	public function get_list_video()
	{
		$this->db->select('*');
		$this->db->where('status_video', '1');
		$query = $this->db->get('video');
		return $query->result();
	}
	
	public function detail_video($id)
	{	
		$this->db->select('
			video.id_video, 
			video.judul_video,
			video.nama_video,
			video.path_video,
			video.sumber_video,
			video.gambar_video,
			video.path_gambar,
			video.deskripsi_video,
			video.info_singkat,
			video.tgl_upload,
			video.status_video, 
			video.file_ext,
			user.nama_lengkap,
			user.username,
			user.nama_foto
		')
		->where('video.id_video', $id)
		->join('user','user.id_user = video.id_user', 'left');
		$query =  $this->db->get('video');
		return $query->result();
	}
	
	public function get_daftar_video()
	{	
		$this->db->select('
			video.id_video, 
			video.judul_video,
			video.sumber_video,
			video.info_singkat,
			video.tgl_upload,
			video.status_video, 
			user.nama_lengkap,
			user.username
		')
		//daerah.nama_daerah)
		->join('user','user.id_user = video.id_user', 'left');
		$query =  $this->db->get('video');
		return $query->result();
	}
		
	public function get_id_video($judul, $nama)
	{
		$this->db->select('id_video');
		$this->db->where('judul_video', $judul);
		$this->db->where('nama_video', $nama);
		$query = $this->db->get('video');
		$row = $query->row();
		return $row->id_video;
	}
	
	public function hapus_video($id_video)
	{	
	
		$detail_video = $this->detail_video($id_video);
		foreach($detail_video as $row){
			$gambar_video = $row->gambar_video;
			$nama_video = $row->nama_video;
		}
		unlink(FCPATH.'upload/image/'.$gambar_video);
		unlink(FCPATH.'upload/video/'.$nama_video);
		
		$this->db->where('id_video', $id_video);
		$this->db->delete('video');
		$this->db->where('id_video', $id_video);
		$this->db->delete('kategori_video');
		return true;
	}
	
	
	public function cari_video_kategori($id_kategori)
	{
		$this->db->select('
			kategori_video.id_kategori,
			kategori.nama_kategori,
			video.judul_video,
			video.nama_video,
			video.sumber_video,
			video.gambar_video,
			video.info_singkat,
			video.tgl_upload,
			video.id_video
		');
		$this->db->where('kategori_video.id_kategori', $id_kategori);
		$this->db->join('video', 'video.id_video = kategori_video.id_video', 'left');
		$this->db->where('video.status_video', '1');
		$this->db->join('kategori', 'kategori.id_kategori = kategori_video.id_kategori', 'left');
		$query = $this->db->get('kategori_video');
		return $query->result();
	}
	
	public function get_cari_video($pencarian)//untuk pencarian di field front end
	{
		$this->db->select('
			video.judul_video,
			video.nama_video,
			video.sumber_video,
			video.gambar_video,
			video.info_singkat,
			video.tgl_upload,
			video.id_video
		');
		$this->db->like('video.judul_video', $pencarian);
		$this->db->where('video.status_video', '1');
		$query = $this->db->get('video');
		return $query->result();
	}
	
	public function cari_video($str) //pencarian di field halaman admin
	{
		$this->db->select('
			video.id_video, 
			video.judul_video,
			video.sumber_video,
			video.info_singkat,
			video.tgl_upload,
			video.status_video, 
			user.nama_lengkap,
			user.username');
		$this->db->like('video.judul_video', $str);
		//$this->db->like('video.info_singkat', $str);
		$this->db->join('user','user.id_user = video.id_user');
		$query = $this->db->get('video');
		return $query->result();
		
	}
	
	public function get_video_user($id_user)
	{
		$this->db->select('*');
		$this->db->where('id_user', $id_user);
		$query = $this->db->get('video');
		$hasil = $query->result();
		return $hasil;
	}
	
	public function get_histori_video()
	{
		$this->db->select('*');
		$this->db->where('status_video', 1);
		$this->db->order_by("id_video", "desc");
		$this->db->limit(10);
		$query = $this->db->get('video');
		$hasil = $query->result();
		return $hasil;
	}
	
	public function get_banyak_video_tidak_aktif()
	{
		$this->db->select('*');
		$this->db->where('status_video', '0');
		$query = $this->db->get('video');
		return $query->num_rows();
	}
	
	public function get_banyak_video_baru()
	{
		$this->db->select('*');
		$this->db->where('video_baru', '0');
		$query = $this->db->get('video');
		return $query->num_rows();
	}
	
	public function get_banyak_video()
	{
		$this->db->select('*');
		$query = $this->db->get('video');
		return $query->num_rows();
	}
	
	public function update_video_baru($id_video)
	{
		$peran = $this->session->userdata('peran');
		if($peran == 0 || $peran == 1){
			$data = array ('video_baru' => '1');
			$this->db->update('video', $data, "id_video = $id_video");
			return true;
		} else {
			return false;		
		}
		
	}
	
	public function validasi_video($id_video)
	{
		$data= array ('status_video' => '1');
		$this->db->update('video', $data, "id_video = $id_video");
		return true;
	}
	// public function get_nama_kategori_video($id)
	// {
		// $this->db->select('
			// kategori.nama_kategori,
			// video.id_video
		// ');
		// $this->db->where('video.id_video', $id);
		// $this->db->join('video', 'video.id_video = kategori_video.id_video', 'left');
		// $this->db->join('kategori', 'kategori.id_kategori = kategori_video.id_kategori', 'left');
		// $query = $this->db->get('kategori_video');
		// $hasil = $query->result();
		// foreach($hasil as $row){
			// return $nama_kategori = $row->nama_kategori;
		// }
	// }
}